package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkPreviewTemplet

import android.content.Intent
import android.os.Handler
import android.text.Html
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TemplateTypeListInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import com.lancoo.znbkxx.teacher.help.KeyboardStateObserverHelp
import kotlinx.android.synthetic.main.aibk_activity_template_preview.*
import kotlinx.android.synthetic.main.aibk_include_layout_template_name.*
import kotlinx.android.synthetic.main.aibk_include_layout_template_overview.*
import kotlinx.android.synthetic.main.aibk_include_loading_refresh.*
import org.koin.androidx.viewmodel.ext.android.viewModel

class AibkTempletPreviewLookActivity: KBaseActivity(), View.OnClickListener{

    private var templetId = ""
    private var templetName = ""
    private var typeCount = 0
    private var itemCount = 0
    private var fullScore = 0.0
    private var answerLength = 0

    companion object {
        fun launch(activity: AppCompatActivity,templetId: String, templetName: String, typeCount: Int, itemCount: Int, fullScore: Double, answerLength: Int) {
            val intent = Intent(activity, AibkTempletPreviewLookActivity::class.java)
            intent.putExtra("TempletId", templetId)
            intent.putExtra("TempletName", templetName)
            intent.putExtra("TypeCount", typeCount)
            intent.putExtra("ItemCount", itemCount)
            intent.putExtra("FullScore", fullScore)
            intent.putExtra("AnswerLength", answerLength)
            activity.startActivity(intent)
        }
    }

    private val mViewModel: MarkViewModel by viewModel()
    private var templateTypeListInfor: TemplateTypeListInfor? = null

    override fun setLayout(): Int {
        return R.layout.aibk_activity_template_preview_look
    }

    override fun initView() {
        IvBack.setOnClickListener(this)
    }

    override fun initData() {
        templetId = intent.getStringExtra("TempletId")
        templetName = intent.getStringExtra("TempletName")
        typeCount = intent.getIntExtra("TypeCount",0)
        itemCount = intent.getIntExtra("ItemCount",0)
        fullScore = intent.getDoubleExtra("FullScore",0.0)
        answerLength = intent.getIntExtra("AnswerLength",0)

        //获取模板管理的选中模板的题型信息
        mViewModel.getTempletTypeList(templetId)
                .doOnSubscribe {
                    RlGifLoading.visibility = View.VISIBLE
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        RlGifLoading.visibility = View.GONE

                        templateTypeListInfor = it.data
                        showPreviewView()
                    } else {
                        MyToast.showToast(it.message)
                        Handler().postDelayed({
                            finish()
                        }, 500)
                    }
                }, {
                    MyToast.showToast(it.message)
                    Handler().postDelayed({
                        finish()
                    }, 500)
                })
    }

    private fun showPreviewView() {
        //显示模范名称
        EdtCustomTemplateName.setText(templetName)
        EdtCustomTemplateName.isFocusable = false

        //显示模板概要
        showTempletTitleInfor(typeCount, itemCount, fullScore)

        //显示模板建议作答时长
        EdtSuggestAnswerTime.setText(answerLength.toString())
        EdtSuggestAnswerTime.isFocusable = false

        //显示模板题型信息
        var manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        RvTemplatePreview.layoutManager = manager
        val mAdapter = AibkPreviewTempletAdapter(this, templateTypeListInfor!!)
        RvTemplatePreview.adapter = mAdapter

    }

    private fun showTempletTitleInfor(totalQuesCount:Int,totalItemCount:Int,totalScore:Double){
        TvTemplateInfor.text = Html.fromHtml(resources.getString(R.string.aibk_templet_infor,
                totalQuesCount, totalItemCount, LancooMethod.killlingDouble(totalScore)))
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.IvBack -> {
                finish()
            }
        }
    }

}